package view;

import java.io.File;

import javax.swing.JFileChooser;

public class FileChooser extends JFileChooser
{
	private static final long serialVersionUID = 1L;

	public FileChooser()
	{
		super();
	}

	public String getDBDirectory()
	{
		this.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		this.setDialogTitle("Locate the files database directory:");
		
		String directory = null;
		
		switch (this.showOpenDialog(new javax.swing.JFrame())) 
		{
			case JFileChooser.APPROVE_OPTION:
				directory = this.getSelectedFile().toString();
				break;
			
			case JFileChooser.CANCEL_OPTION:
				
				directory = "cancel";
				break;
				
			case JFileChooser.ERROR_OPTION:
				directory = null;
				break;
		}
		
		if( (directory != null) && (!directory.equals("cancel")) && (new File(directory).isDirectory()) ) 
		{
			return directory;
		}
		else if(directory.equals("cancel"))
			return directory;			
		else
			return null;

	}
	
	public String getQueryFile()
	{
		this.setFileSelectionMode(JFileChooser.FILES_ONLY);
		this.setDialogTitle("Locate the query's file:");
		
		String file = null;
		
		switch (this.showOpenDialog(new javax.swing.JFrame())) 
		{
			case JFileChooser.APPROVE_OPTION:
				file = this.getSelectedFile().toString();
				break;
			
			case JFileChooser.CANCEL_OPTION:
				
				file = "cancel";
				break;
				
			case JFileChooser.ERROR_OPTION:
				file = null;
				break;
		}
		
		if( (file != null) && (!file.equals("cancel")) && (new File(file).exists()) ) 
		{
			return file;
		}
		else if(file.equals("cancel"))
			return file;			
		else
			return null;

	}
}
